var index=1;
define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'facrm/apps/email/index',
                    add_url: 'facrm/apps/email/add',
                    edit_url: 'facrm/apps/email/edit',
                    del_url: 'facrm/apps/email/del',
                    multi_url: '',
                    table: '',
                }
            });

            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {field: 'id', title: 'ID'},
                        {field: 'describe', title: __('名称'), operate: "LIKE"},
                        {field: 'values', title: __('配置JSON'), operate: "LIKE",visible:false,},
                        {
                            field: 'status',
                            title: __('Status'),
                            formatter: Table.api.formatter.status,
                            searchList: {0: __('禁用'), 1: __('正常')}
                        },
                        {
                            field: 'create_time',
                            title: __('创建时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true,
							extend: 'autocomplete="off"'
                        },
                        {
                            field: 'update_time',
                            title: __('修改时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true,
							extend: 'autocomplete="off"'
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                        }

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();

        },
        edit: function () {
            Controller.api.bindevent();
        },
        send: function () {
            Controller.api.bindevent();
            $(document).on("change", "#c-email_id", function(){
                var email_id=$(this).val();
                var content=$("#c-content").val();

                var url=$("#location_url").val();
                var data_param=Controller.api.func.getUrlParam('data');
                url=url+"?data="+data_param;
                if (content){
                    Layer.confirm(
                        __('邮件已经存在内容，确定要覆盖吗？'),
                        {icon: 3, title: __('Warning'), shadeClose: true},
                        function (index) {
                            //为了兼容多个富文本用跳转形式
                            location=url+"&email_id="+email_id
                            layer.close(index);
                            return;
                        }
                    );
                }else{
                    location=url+"&email_id="+email_id;
                    return;
                }
                console.log(content);
            });
        },
        sends: function () {
            Controller.api.bindevent();
            $(document).on("change", "#c-email_id", function(){
                var email_id=$(this).val();
                var content=$("#c-content").val();

                var url=$("#location_url").val();
                var data_param=Controller.api.func.getUrlParam('data');
                url=url+"?data="+data_param;
                if (content){
                    Layer.confirm(
                        __('邮件已经存在内容，确定要覆盖吗？'),
                        {icon: 3, title: __('Warning'), shadeClose: true},
                        function (index) {
                            //为了兼容多个富文本用跳转形式
                            location=url+"&email_id="+email_id
                            layer.close(index);
                            return;
                        }
                    );
                }else{
                    location=url+"&email_id="+email_id;
                    return;
                }
                console.log(content);
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
                $("input[type=radio][name='row[config]']").change(function(){
                    var type=$(this).val();
                    if(type==1){
                        $('.table-responsive').show();
                    }else{
                        $('.table-responsive').hide();
                    }
                });
            },
            func: {
                getUrlParam(name) {
                    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
                    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
                    if (r != null) return unescape(r[2]); return null; //返回参数值
                }

            }
        }
    };
    return Controller;
});


